<?php
namespace Index\Controller;
use Common\Sms\Sms;
use Common\Alipay\Pcpay;
use Common\Alipay\Wappay;
use Think\Controller;


class IndexController extends Controller {
	
   
	
	function index(){
		$this->display();
	}
	
	function ording(){
		$data=I('get.');
		$order=base64_decode($data['order']);
		$where=array(
			'order'=>$order
		);
		$list=M('order')->where($where)->find();
		$this->assign('list',$list);
		$this->display();
	}
	
	function pushorder(){
		$data=I('post.');
		$username=$data['username'];
		$phone=$data['phone'];
		$email=$data['email'];
		$product=$data['product'];
		$vis=$data['vis'];
		$time=time();
		
		$order=date('YmdHis',time());
		$viss=M('token')->where(array('phone'=>$phone))->find();
		if($viss['token']!=$vis){
			$this->ajaxReturn(array('s'=>false,'m'=>"验证码错误"));
		}
		$vistt=$time-$viss['time'];
		if($vistt>=900 && $viss['status']==1){
			$this->ajaxReturn(array('s'=>false,'m'=>"验证码失效"));
		}
		$data=array(
			'name'=>$username,
			'phone'=>$phone,
			'email'=>$email,
			'product'=>$product,
			'order'=>$order,
			'time'=>$time,
		);		
		$viss=M('token')->where(array('phone'=>$phone))->save(array('status'=>1));
		$res=M('order')->add($data);
		$order=base64_encode($order);
		if($res){
			$this->ajaxReturn(array('s'=>true,'d'=>$order));
		}
	}
	//发送验证短信
    function msg(){
        $phone=trim(I('phone'));
		$tempid="SMS_116567434";
		$num=$this->randString();
		$param=array("code"=>$num);
		$where=array('phone'=>$phone);
		
		$response = Sms::sendSms($phone,$tempid,$param);
		$response=json_decode(json_encode($response),true);
		if($response['Code']=="OK"){
			$list=M('token')->where($where)->find();
	        if (!empty($list)) {
	        	
	        	$data=array(
	        		'token'=>$num,
	        		'status'=>0,
	        		'time'=>time()
				);
	        	$res=M('token')->where($where)->save($data);
	        }else{
	        	
	        	$data=array(
	        		'token'=>$num,
	        		'phone'=>$phone,
	        		'time'=>time(),
				);
				$res=M('token')->add($data);	
	        }
			if($res){
				$this->ajaxReturn(array('s'=>true,'m'=>$response['Message']));
			}	
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>$response['Message']));
		}   
	}


    /**
     * 获取随机位数数字
     * @param  integer $len 长度
     * @return string       
     */
    function randString($len = 4)
    {
        $chars = str_repeat('0123456789', $len);
        $chars = str_shuffle($chars);
        $str   = substr($chars, 0, $len);
        return $str;
    }

	function pcpay(){
		$data=I('get.');
		$res=M('order')->where(array('order'=>$data['order']))->find();
		if($res){
			//商户订单号，商户网站订单系统中唯一订单号，必填
			$out_trade_no =$res['order'];
		    //订单名称，必填
		    $subject = $res['product'];
		    //付款金额，必填
		    $total_amount = '36666';
		    //商品描述，可空
		    $body = $res['product'];
			$response = Pcpay::pay($out_trade_no,$subject,$total_amount,$body);
			var_dump($response);
		}else{
			P("订单不存在");	
		}
	}
	
	function wappay(){
		$data=I('get.');
		$res=M('order')->where(array('order'=>$data['order']))->find();
		if($res){
			//商户订单号，商户网站订单系统中唯一订单号，必填
			$out_trade_no =$res['order'];
		    //订单名称，必填
		    $subject =  $res['product'];
		    //付款金额，必填
		    $total_amount = '36666';
		    //商品描述，可空
		    $body =  $res['product'];
			$response = Wappay::pay($out_trade_no,$subject,$total_amount,$body);
		}else{
			P("订单不存在");	
		}
	}
	

	//异步验签
	function notify(){
		$arr=$_POST;
		$response = Pcpay::notify_url($arr);
		if($response){
			//验签成功业务逻辑
			$res=M('order')->where(array('order'=>$_POST['out_trade_no']))->find();
			if($res){
				$data=array(
	    		'notify_time'=>$_POST['notify_time'],
	    		'notify_type'=>$_POST['notify_type'],
	    		'notify_id'=>$_POST['notify_id'],
	    		'charset'=>$_POST['charset'],
	    		'version'=>$_POST['version'],
	    		'sign_type'=>$_POST['sign_type'],
	    		'sign'=>$_POST['sign'],
	    		'auth_app_id'=>$_POST['auth_app_id'],
	    		'trade_no'=>$_POST['trade_no'],
	    		'app_id'=>$_POST['app_id'],
	    		'out_trade_no'=>$_POST['out_trade_no'],
	    		'out_biz_no'=>$_POST['out_biz_no'],
	    		'buyer_id'=>$_POST['buyer_id'],
	    		'seller_id'=>$_POST['seller_id'],
	    		'trade_status'=>$_POST['trade_status'],
	    		'total_amount'=>$_POST['total_amount'],
				);
				$list=M('order_msg')->add($data);
			}
			
			echo "success";	//请不要修改或删除	
		}else {
		    echo "fail";	
		}
	}
	//同步验签
	function returnco(){
		 
		$arr=$_GET;
		$response = Pcpay::return_url($arr);
		if($response){
			//验签成功业务逻辑
			$res=M('order')->where(array('order'=>$arr['out_trade_no']))->find();
			$this->msg1($res['phone'],$res['name'],$res['product'],$res['order']);
			$this->msg2($res['name'],$res['phone'],$res['product']);
			if($res['paystate']==0){
				$data=array(
	        		'paystate'=>1,
				);
				$list=M('order')->where(array('order'=>$arr['out_trade_no']))->save($data);
				if($list){
					 header("Location:http://www.ltjc66.com"); 
				}
			}
			
		}
	}
	
	function check(){
		$data=I('post.');
		$res=M('order')->where(array('order'=>$data['order']))->find();
		if($res['paystate']==1){
			$this->ajaxReturn(array('s'=>true));
		}else{
			$this->ajaxReturn(array('s'=>false));
		}
		
		
	}


	//发送验证短信
    function msg1($phone,$name,$product,$code){
		$tempid="SMS_116562563";
		$param=array(
			"name"=>$name,
			"product"=>$product,
			"code"=>$code,
		);
		$response = Sms::sendSms($phone,$tempid,$param);
	}
	function msg2($name,$usrephone,$product){
      	$phone="18172243663";
		$tempid="SMS_116592702";
		$param=array(
			"name"=>$name,
			"phone"=>$usrephone,
			"product"=>$product,
		);
		$response = Sms::sendSms($phone,$tempid,$param);
	}
	
}